package org.jeecg.modules.sg.service.impl;

import org.jeecg.modules.sg.entity.SgBerth;
import org.jeecg.modules.sg.entity.SgChangeRoom;
import org.jeecg.modules.sg.entity.SgInRoom;
import org.jeecg.modules.sg.entity.SgPerson;
import org.jeecg.modules.sg.mapper.SgChangeRoomMapper;
import org.jeecg.modules.sg.service.ISgBerthService;
import org.jeecg.modules.sg.service.ISgChangeRoomService;
import org.jeecg.modules.sg.service.ISgPersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @Description: 调宿申请表
 * @Author: jeecg-boot
 * @Date:   2022-09-29
 * @Version: V1.0
 */
@Service
public class SgChangeRoomServiceImpl extends ServiceImpl<SgChangeRoomMapper, SgChangeRoom> implements ISgChangeRoomService {
    @Autowired
    public ISgPersonService iSgPersonService;
    @Autowired
    public ISgBerthService iSgBerthService;

    @Override
    @Transactional
    public void audSgInRooms(String[] ids) {
        for (String id:ids){
            SgChangeRoom sgChangeRoom=this.getById(id);
            if("ysh".equals(sgChangeRoom.getStatus())){
            }else{
                sgChangeRoom.setStatus("ysh");
                sgChangeRoom.setAudDate(new Date());//审核时间
                this.updateById(sgChangeRoom);
                SgPerson sgPerson=iSgPersonService.getById(sgChangeRoom.getPresonId());
                //将本记录更新为退宿
                sgPerson.setStatus("ts");
                sgPerson.setOutRoomDate(sgChangeRoom.getChangeDate());
                iSgPersonService.updateById(sgPerson);
                //复制当前的人员新增后换到另一个床位
                sgPerson.setId(null);
                sgPerson.setInRoomDate(sgChangeRoom.getChangeDate());
                sgPerson.setOutRoomDate(null);
                sgPerson.setRoomId(sgChangeRoom.getRoomId());
                sgPerson.setBerthId(sgChangeRoom.getBerthId());
                sgPerson.setChangeTime(sgPerson.getChangeTime()==null?1:sgPerson.getChangeTime()+1);
                iSgPersonService.save(sgPerson);
                //修改床位的状态
                SgBerth oldSgBerth=iSgBerthService.getById(sgChangeRoom.getOldBerthId());
                oldSgBerth.setPersonId(null);
                iSgBerthService.updateById(oldSgBerth);
                SgBerth sgBerth=iSgBerthService.getById(sgChangeRoom.getBerthId());
                sgBerth.setPersonId(sgPerson.getId());
                iSgBerthService.updateById(sgBerth);
            }
        }
    }
}
